package com.example.jeong.yuejian10.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.jeong.yuejian10.Bean.AttentionBean;
import com.example.jeong.yuejian10.Bean.IsAttentionBean;
import com.example.jeong.yuejian10.Model.ActivityModel;
import com.example.jeong.yuejian10.R;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.jeong.yuejian10.constant.Constant.ACTIVITY_PHOTO_URL;
//俱乐部内的活动的详情页
public class ClubActivityDetailActivity extends AppCompatActivity {

    private ImageView backlist;
    private ImageView collect;
    private ImageView activity_pic;
    private TextView a_time;
    private TextView a_phone;
    private TextView a_name;
    private TextView a_price;
    private TextView a_address;

    private Button baoming;
    private int u_id;
    private int a_id;
    private int select_id;
    private String flag;
   private String coresult="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_detail);
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id",0);
        initViews();
        initEvents();
        getActivityData();
        isBeCollected();
    }

    private void initViews(){
        collect = (ImageView) findViewById(R.id.collect);
        backlist = (ImageView) findViewById(R.id.back_activitylist);
        a_name = (TextView) findViewById(R.id.a_name);
        a_address = (TextView) findViewById(R.id.a_address);
        a_time = (TextView) findViewById(R.id.a_time);
        a_phone = (TextView) findViewById(R.id.a_phone);
        a_price = (TextView) findViewById(R.id.a_price);
        baoming = (Button) findViewById(R.id.baoming);
        activity_pic = (ImageView) findViewById(R.id.activity_pic);
    }
    private void initEvents(){
        //返回列表页
        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        final Bundle bundle = this.getIntent().getExtras();
        final int a_id=bundle.getInt("a_id");
        baoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClubActivityDetailActivity.this, EnterActActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("a_id", a_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectAct();
            }
        });
    }
    private void getActivityData(){
        Bundle bundle = this.getIntent().getExtras();
        int a_id = bundle.getInt("a_id");
        String aname = bundle.getString("a_name");
        String aaddress = bundle.getString("a_address");
        String aphone = bundle.getString("a_phone");
  int aprice = bundle.getInt("a_price");
        String atime = bundle.getString("a_time");
        String apic = ACTIVITY_PHOTO_URL+bundle.getString("a_picture");

        a_name.setText(aname);
        a_address.setText(aaddress);
        a_phone.setText(aphone);
      a_price.setText(aprice+"");
        a_time.setText(atime);
        Picasso.with(this).load(apic).into(activity_pic);
    }

    private void isBeCollected(){
        //判断是否关注
        ActivityModel activityModel=new ActivityModel();
        Call<IsAttentionBean> callIsCollect=activityModel.getIsCollected(u_id,a_id,"1");
        callIsCollect.enqueue(new Callback<IsAttentionBean>() {
            @Override
            public void onResponse(Call<IsAttentionBean> call, Response<IsAttentionBean> response) {
                String result=response.body().getCollected();
                if ("0".equals(result)) {

                    collect.setImageResource(R.drawable.ic_collect_hui);
                    coresult = "no";
                } else {
                    coresult = "yes";
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
            }

            @Override
            public void onFailure(Call<IsAttentionBean> call, Throwable t) {

            }
        });

    }

    //关注活动
    private void collectAct(){
       ActivityModel activityModel=new ActivityModel();
        Call<AttentionBean> callCollect=activityModel.userCollectActivity(u_id, a_id);
        callCollect.enqueue(new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {
                if ("yes".equals(coresult)) {
                    Toast.makeText(ClubActivityDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_hui);
                }
                else{
                    Toast.makeText(ClubActivityDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
                refresh();
            }

            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {

            }

        });
    }
    private void refresh(){
        isBeCollected();
    }
}
